<?php
class Core_Data_Mappers_Training extends Core_Data_Mappers_Abstract {
	protected $_dbTable;
	protected $_map=array("Id"=>"id","Name"=>"name","Detail"=>"detail","Created"=>"created","Updated"=>"updated","CreatedBy"=>"CreatedBy","Paymentid"=>"paymentid");
	public function __construct() {
		$this->_businessclass ='Core_Business_Training';
		$this->_modelclass ='Core_Data_Models_Training';
	}
	
	public function find($id) {
		$row = $this->getDbTable()->fetchAll(array("id = ?"=>$id));
		if(isset($row) && count($row)>0) 
			return $this->map($row->current());
		else 
			return null;		
	}
	
	/***
	 * get the top $count training object in database.
	 * 
	 * @param $where array array with the filter expression
	 * @param $count int max number of training per query default(10)
	 * @param $start int begin to select object at start position default(0)
	 * @return array|null array of Core_Business_Training 
	 */
	public function fetchAll($where=null,$count=10,$start=0) {
		$rows = $this->getDbTable()->fetchAll($where,null,null,$start);
		if (isset($rows) && $rows->current() !==null) {
			$output = array();
			foreach ($rows as $row) {
				$output[]= $this->map($row);
			}
			return $output;
		}
		return null;
	}
	/*public static function getById($id) {
		
		$row = $this->getDbTable()->fetchAll(array("id = ?"=>$id));
		if(isset($row) && count($row)>0) 
			return $this->map($row->current());
		else 
			return null;
	}*/
	
	public function add($object){
		if($object instanceof $this->_businessclass && isset($object->Id)==0) {
			$data= $this->reversemap($object);
			unset($data['id']);
			$id = $this->getDbTable()->insert($data);
			$object->setId($id);
		}
	}
	
	public function edit($object){
		if($object instanceof $this->_businessclass) {
			$data = $this->reversemap($object);
			unset($data['id']);
			$this->getDbTable()->update($data, array("id = ?"=>$object->Id));
		}	
	}
	public function exists($object) {
		if($object instanceof $this->_businessclass && $object->Id <> '') {
				$obj = $this->find($object->Id);
				if($obj!==null && $obj instanceof $this->_businessclass)
					return true;
				else 
					return false;
		}
		else 
			return false;
	}
}
?>